<template>

    <el-table :data="state.data" height="900"
    :default-sort="state.sort"
    @sort-change="sortChange"
    :cell-class-name="tableCellClassName"
    @cell-click="cellClick"
    style="width: 100%">

        <el-table-column prop="date" label="日期" width="120" sortable/>
        <el-table-column prop="day" label="星期" width="80">
            <template #default="scope">
                {{ getDayOfWeek(scope.row.date) }}
            </template> 
        </el-table-column>
        <el-table-column prop="stock" label="股票" width="150">
            <template #default="scope">
                <ToStockDetail :stock="scope.row.stock" />
            </template> 
        </el-table-column>
        <el-table-column prop="industry" label="industry" width="160"/>
        <el-table-column prop="close" label="close" sortable/>
        <el-table-column prop="pct" label="pct" sortable/>
        <el-table-column prop="nextPct" label="nextPct" sortable/>
        <el-table-column prop="pct3D" label="pct3D" width="80" sortable />
        <el-table-column prop="pct5D" label="pct5D" width="80" sortable />
        <el-table-column prop="lp" label="lp" sortable/>
        <el-table-column prop="gt20d" label="gt20d" sortable width="100"/>
        <el-table-column prop="gt30d" label="gt30d" sortable width="100"/>
        <el-table-column prop="gt60d" label="gt60d" sortable width="100"/>
        <el-table-column prop="gt300d" label="gt300d" sortable width="100"/>
        <el-table-column prop="moneyRank" label="moneyRank" sortable width="100"/>
        <el-table-column prop="volumeRank" label="volumeRank" sortable width="100"/>
    </el-table>
</template>

<script>
// 个股周数据详情页
import { reactive } from 'vue';
import { getTradingRankGt5, getTradingRankLt5 } from '../../service/tradingRank';
import ToStockDetail from '../ToStockDetail.vue';
import { getLpClassName,getDayOfWeek } from '../../utils/help';

export default {
    name: 'TradingRank',
    components: {
        ToStockDetail,
    },
    setup() {

        const baseKey = "tradingRankLt5"
        const sortPropKey = baseKey + "SortProp"
        const sortOrderKey = baseKey + "SortOrder"

        const state = reactive({
            rawData: [],
            data: [],
            operationSelected: "",
            sort: { 
                prop: sessionStorage.getItem(sortPropKey) || 'date' , 
                order: sessionStorage.getItem(sortOrderKey) || 'descending' 
            }
        })

        getTradingRankLt5().then(res => {
            state.data = res.data
            state.rawData = res.data
        })

        const sortChange = (column, prop, order) => {
            sessionStorage.setItem(sortPropKey, column.prop)
            sessionStorage.setItem(sortOrderKey, column.order)
        }

        const tableCellClassName = ({row, column, rowIndex, columnIndex}) => {
            var columnProperty = column.property
            if (columnProperty == "lp") {
                return getLpClassName(row[columnProperty])
            }
            if (["nextPct"].includes(columnProperty)) {
                if (row[columnProperty] < 0) {
                    return 'lt0-cell'
                }             
            }

            if (["gt60d","gt300d", "gt30d","gt20d","lowGt20d","lowGt30d"].includes(columnProperty)) {
                if (row[columnProperty] >= 0) {
                    return 'd300up-cell'
                } else {
                    return 'd300down-cell'
                }
            }

            return ''
        }

        const cellClick = (row, column, cell, event) => {
            var columnProperty = column.property

            if (['industry','date','minDate','maxDate','curPct','cur60wPct','nextWeekPct'].includes(columnProperty)) {
                let val = row[columnProperty]
                if (state.operationSelected != "" && state.operationSelected == val) {
                    state.operationSelected = ""
                    state.data = state.rawData
                } else {
                    state.operationSelected = val
                    if (["curPct","cur60wPct","nextWeekPct"].includes(columnProperty)) {
                        if (val >= 0) {
                            state.data = state.rawData.filter(item => item[columnProperty] >= 0)
                        } else {
                            state.data = state.rawData.filter(item => item[columnProperty] < 0)
                        }
                    } else {
                        state.data = state.rawData.filter(item => item[columnProperty] == val)
                    }
                }
            }
        }

        return {state, tableCellClassName, sortChange, getDayOfWeek, cellClick}
    }
}
</script>